package com.eastfair.notice.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.eastfair.enums.MsgTypeEnum;
import com.eastfair.notice.dto.MsgSendDTO;
import com.eastfair.notice.dto.MsgMqDTO;
import com.eastfair.notice.entity.Notice;
import com.eastfair.notice.service.NoticeService;
import com.eastfair.projectcore.entity.MsgTemplate;
import com.eastfair.third.jpush.JPushSendUtil;
import com.eastfair.third.jpush.model.JPushConfig;
import com.eastfair.third.jpush.model.JPushInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 站内信发送mq消费
 * @ClassName NoticeSendListener
 * @Description TODO
 * @Author liuxiaobai
 * @Date 2021/8/9 2:05 下午
 * @Version 1.0
 */
@Component
public class NoticeSendListener {
    private static final Logger logger = LoggerFactory.getLogger(NoticeSendListener.class);

    @Autowired
    private NoticeService noticeService;

    @JmsListener(destination = "${activemq.queue.noticeSend}")
    public void handleMessage(String json) {
        try {
            MsgMqDTO msg = JSON.parseObject(json, MsgMqDTO.class);
            JPushConfig config = msg.getJPushConfig();
            MsgSendDTO send = msg.getMsgSendDTO();
            MsgTemplate template = msg.getTemplate();
            if (send == null || config == null || template == null) {
                return;
            }
            //保存站内信
            Notice notice = Notice.builder()
                    .projectId(send.getProjectId())
                    .exhibitionId(send.getExhibitionId())
                    .receiveType(send.getReceiveType().name())
                    .receiveId(send.getReceiveUserId())
                    .businessType(send.getSubjectType() != null ? send.getSubjectType().name() : null)
                    .businessId(send.getBusinessId())
                    .content(template.getContent())
                    .enContent(template.getEnContent())
                    .noticeType(send.getNoticeType().name())
                    .build();
            this.noticeService.save(notice);

            //发送极光
            JPushInfo jpushInfo = JPushInfo.builder()
                    .appKey(config.getAppKey())
                    .masterSecret(config.getMasterSecret())
                    .timeToLive(config.getTimeToLive())
                    .build();
            //创建map
            Map<String, String> map = new HashMap<>(5);
            map.put("exhibitionId", String.valueOf(send.getExhibitionId()));
            map.put("type", MsgTypeEnum.NOTICE.name());
            map.put("noticeType", send.getNoticeType().name());
            //业务ID
            map.put("businessId", String.valueOf(send.getBusinessId()));
            //接收人类型
            map.put("subjectType", send.getReceiveType().name());
            jpushInfo.setMap(map);
            //userAccountId 别名
            jpushInfo.setAlias(String.valueOf(send.getReceiveUserId()));
            jpushInfo.setContent(notice.getContent());
            JPushSendUtil.buldPushObject_all_all_alias(jpushInfo);
        } catch (Exception e) {
            logger.error("站内信mq消费",e);
        }

    }

}
